/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConfigPanel.java
 *
 * Created on 21 Feb 2011, 10:27:10 AM
 */
package za.co.pas.business.view.panel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.event.ListSelectionEvent;
import za.co.pas.business.constant.Constant;
import za.co.pas.business.data.Country;
import za.co.pas.business.data.Province;
import za.co.pas.business.data.UserGroups;
import za.co.pas.business.data.UserType;
import za.co.pas.business.model.ConfDataModel;
import za.co.pas.business.model.CountryDataModel;
import za.co.pas.business.mvc.BusinessMVC;
import za.co.pas.business.view.dlg.GenericDataDialog;
import za.co.pas.business.view.model.list.CountryListModel;
import za.co.pas.business.view.model.list.ProvinceListModel;
import za.co.pas.business.view.model.list.UserGroupListModel;
import za.co.pas.business.view.model.list.UserTypeListModel;

/**
 *
 * @author alabuschagne
 */
public class ConfigListPanel extends javax.swing.JPanel
{

    private UserGroupListModel userGroupListModel = new UserGroupListModel();
    private UserTypeListModel userTypeListModel = new UserTypeListModel();
    private CountryListModel countryListModel = new CountryListModel();
    private ProvinceListModel provinceListModel = new ProvinceListModel();

    /** Creates new form ConfigPanel */
    public ConfigListPanel()
    {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        userGroupsList = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        userTypesList = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        countryList = new javax.swing.JList();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        provinceList = new javax.swing.JList();

        setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "System", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "User Groups", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        userGroupsList.setModel(userGroupListModel);
        userGroupsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        userGroupsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                userGroupsListMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(userGroupsList);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "User Types", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        userTypesList.setModel(userTypeListModel);
        userTypesList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        userTypesList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                userTypesListMouseReleased(evt);
            }
        });
        jScrollPane2.setViewportView(userTypesList);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Countries", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        countryList.setModel(countryListModel);
        countryList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        countryList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                countryListMouseReleased(evt);
            }
        });
        countryList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                countryListValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(countryList);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane3)
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Provinces", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        provinceList.setModel(provinceListModel);
        provinceList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        provinceList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                provinceListMouseReleased(evt);
            }
        });
        jScrollPane4.setViewportView(provinceList);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane4)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void userGroupsListMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_userGroupsListMouseReleased
    {//GEN-HEADEREND:event_userGroupsListMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();

            JMenuItem menuItem = new JMenuItem("New");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editUserGroupList(null);
                }
            });
            int k = userGroupsList.getSelectedIndex();
            if (k != -1)
            {
                menuItem = new JMenuItem("Edit");
                Pmenu.add(menuItem);
                menuItem.addActionListener(new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        int k = userGroupsList.getSelectedIndex();
                        if (k != -1)
                        {
                            UserGroups ug = (UserGroups) userGroupListModel.getElementAt(k);
                            editUserGroupList(ug);
                        }
                    }
                });
            }
            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_userGroupsListMouseReleased

    private void userTypesListMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_userTypesListMouseReleased
    {//GEN-HEADEREND:event_userTypesListMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();

            JMenuItem menuItem = new JMenuItem("New");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editUserTypeList(null);
                }
            });
            int k = userTypesList.getSelectedIndex();
            if (k != -1)
            {
                menuItem = new JMenuItem("Edit");
                Pmenu.add(menuItem);
                menuItem.addActionListener(new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        int k = userTypesList.getSelectedIndex();
                        if (k != -1)
                        {
                            UserType ug = (UserType) userTypeListModel.getElementAt(k);
                            editUserTypeList(ug);
                        }
                    }
                });
            }
            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_userTypesListMouseReleased

    private void countryListMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_countryListMouseReleased
    {//GEN-HEADEREND:event_countryListMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();

            JMenuItem menuItem = new JMenuItem("New");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editCountryList(null);
                }
            });
            int k = countryList.getSelectedIndex();
            if (k != -1)
            {
                menuItem = new JMenuItem("Edit");
                Pmenu.add(menuItem);
                menuItem.addActionListener(new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        int k = countryList.getSelectedIndex();
                        if (k != -1)
                        {
                            Country c = (Country) countryListModel.getElementAt(k);
                            editCountryList(c);
                        }
                    }
                });
            }
            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_countryListMouseReleased

    private void countryListValueChanged(javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_countryListValueChanged
    {//GEN-HEADEREND:event_countryListValueChanged
        int k = countryList.getSelectedIndex();
        if (k != -1)
        {
            Country c = (Country) countryListModel.getElementAt(k);
            provinceListModel.setCountry(c);
            provinceList.updateUI();
        }
    }//GEN-LAST:event_countryListValueChanged

    private void provinceListMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_provinceListMouseReleased
    {//GEN-HEADEREND:event_provinceListMouseReleased
        if (evt.isPopupTrigger())
        {
            Country country = null;
            int kc = countryList.getSelectedIndex();
            if (kc != -1)
            {
                country = (Country) countryListModel.getElementAt(kc);
            }
            if (country == null)
            {
                return;
            }
            final Country country2 = country;
            JPopupMenu Pmenu = new JPopupMenu();

            JMenuItem menuItem = new JMenuItem("New");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editProvinceList(country2, null);
                }
            });
            int k = provinceList.getSelectedIndex();
            if (k != -1)
            {
                menuItem = new JMenuItem("Edit");
                Pmenu.add(menuItem);
                menuItem.addActionListener(new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        int k = provinceList.getSelectedIndex();
                        if (k != -1)
                        {
                            Province c = (Province) provinceListModel.getElementAt(k);
                            editProvinceList(country2, c);
                        }
                    }
                });
            }
            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_provinceListMouseReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList countryList;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JList provinceList;
    private javax.swing.JList userGroupsList;
    private javax.swing.JList userTypesList;
    // End of variables declaration//GEN-END:variables

    private void editUserGroupList(UserGroups ug)
    {
        ConfDataModel cdm = (ConfDataModel) Constant.mvc.getModel(BusinessMVC.CONF_DATA_MODEL);
        GenericDataDialog gdd = new GenericDataDialog(Constant.getParentFrame(this), true);
        if (ug != null)
        {
            gdd.setGenericData(ug.clone());
        }
        gdd.setVisible(true);
        if (gdd.isOk())
        {
            if (ug == null)
            {
                ug = new UserGroups();
                cdm.add(ug);
            }
            UserGroups ug2 = (UserGroups) gdd.getGenericData();
            ug.setName(ug2.getName());
            ug.setDescription(ug2.getDescription());
            //persist
            try
            {
                cdm.persistGenericObject(ug);
            }
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
            userGroupsList.updateUI();
        }
    }

    private void editUserTypeList(UserType ut)
    {
        ConfDataModel cdm = (ConfDataModel) Constant.mvc.getModel(BusinessMVC.CONF_DATA_MODEL);
        GenericDataDialog gdd = new GenericDataDialog(Constant.getParentFrame(this), true);
        if (ut != null)
        {
            gdd.setGenericData(ut.clone());
        }
        gdd.setVisible(true);
        if (gdd.isOk())
        {
            if (ut == null)
            {
                ut = new UserType();
                cdm.add(ut);
            }
            UserGroups ut2 = (UserGroups) gdd.getGenericData();
            ut.setName(ut2.getName());
            ut.setDescription(ut2.getDescription());
            //persist
            try
            {
                cdm.persistGenericObject(ut);
            }
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
            userTypesList.updateUI();
        }
    }

    private void editCountryList(Country country)
    {
        CountryDataModel cdm = (CountryDataModel) Constant.mvc.getModel(BusinessMVC.COUNTRY_DATA_MODEL);
        GenericDataDialog gdd = new GenericDataDialog(Constant.getParentFrame(this), true);
        if (country != null)
        {
            gdd.setNameField("Country code:", country.getCode());
            gdd.setDescriptionField("Name:", country.getName());
        }
        else
        {
            gdd.setNameField("Country code:", "");
            gdd.setDescriptionField("Name:", "");
        }
        gdd.setVisible(true);
        if (gdd.isOk())
        {
            if (country == null)
            {
                country = new Country();
                cdm.add(country);
            }
            country.setCode(gdd.getNameField());
            country.setName(gdd.getDescriptionField());
            //persist
            try
            {
                cdm.persistGenericObject(country);
            }
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
            countryList.updateUI();
        }
    }

    private void editProvinceList(Country country, Province province)
    {
        CountryDataModel cdm = (CountryDataModel) Constant.mvc.getModel(BusinessMVC.COUNTRY_DATA_MODEL);
        GenericDataDialog gdd = new GenericDataDialog(Constant.getParentFrame(this), true);
        if (province != null)
        {
            gdd.setNameField("Province code:", province.getCode());
            gdd.setDescriptionField("Name:", province.getName());
        }
        else
        {
            gdd.setNameField("Province code:", "");
            gdd.setDescriptionField("Name:", "");
        }
        gdd.setVisible(true);
        if (gdd.isOk())
        {
            if (province == null)
            {
                province = new Province();
                cdm.add(country, province);
            }
            province.setCode(gdd.getNameField());
            province.setName(gdd.getDescriptionField());
            //persist
            try
            {
                cdm.persistGenericObject(province);
                cdm.persistGenericObject(country);
            }
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
            countryList.updateUI();
        }
    }
}
